package com.zulu.karaoke.gui;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.zulu.karaoke.R;
import com.zulu.karaoke.adapter.ListSongAdapter;
import com.zulu.karaoke.database.DatabaseManager;
import com.zulu.karaoke.database.SongTableHelper;
import com.zulu.karaoke.gui.custom.CustomAlertDialog;
import com.zulu.karaoke.utils.GeneralUtils;

public class FragmentFullListSearch extends Fragment {

	private static FragmentFullListSearch instance;
	private Context mContext;

	private EditText mEdtTextSearch;
	private Button mBtnClearText;
	private Button mBtnSearchInternet;
	private TextView mTvSearchNoResult;
	private ListView mLstViewSearch;
	private SongTableHelper insSongHelper;
	private List<String> lstNumber;
	private List<String> lstTitle;

	private ListSongAdapter adapter;

	public static FragmentFullListSearch getInstance() {
		if (instance == null) {
			instance = new FragmentFullListSearch();
		}
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// return super.onCreateView(inflater, container, savedInstanceState);
		final View view = inflater.inflate(R.layout.fragment_full_search_list, null);

		mContext = this.getActivity();
		insSongHelper = DatabaseManager.getInstance()
				.getSongTableHelper();

		mEdtTextSearch = (EditText) view.findViewById(R.id.edt_search_text);
		mBtnSearchInternet = (Button) view.findViewById(R.id.btn_search_for_internet);
		mBtnSearchInternet.setVisibility(View.INVISIBLE);
		mBtnClearText = (Button)view.findViewById(R.id.btn_clear_search_text);
		mTvSearchNoResult = (TextView)view.findViewById(R.id.tv_search_no_result);
		mBtnClearText.setVisibility(View.GONE);
		mLstViewSearch = (ListView) view.findViewById(R.id.lst_view_search);
		mLstViewSearch.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				GeneralUtils.showPopupSongDetail(mContext, insSongHelper,
						lstNumber, position, adapter);
			}
		});

		mEdtTextSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if(s.toString().trim().length()==0){
//					mBtnSearchInternet.setEnabled(false);
					mBtnSearchInternet.setVisibility(View.INVISIBLE);
					mLstViewSearch.setAdapter(null);
					mBtnClearText.setVisibility(View.GONE);
					mTvSearchNoResult.setVisibility(View.GONE);
				} else {
//					mBtnSearchInternet.setEnabled(true);
					mBtnSearchInternet.setVisibility(View.VISIBLE);
					adapter = new ListSongAdapter(mContext, getAllSongNumberOnFilter(s.toString()), 
							getAllSongTitleOnFilter(s.toString()));
					mLstViewSearch.setAdapter(adapter);
					mBtnClearText.setVisibility(View.VISIBLE);
					if(adapter.getCount() == 0) {
						mTvSearchNoResult.setVisibility(View.VISIBLE);
						mTvSearchNoResult.setText(getActivity().getString(R.string.mess_search_no_result));
					} else {
						mTvSearchNoResult.setVisibility(View.GONE);
					}
				}
			}
		});
		
		mBtnClearText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				mBtnSearchInternet.setEnabled(false);
				mBtnSearchInternet.setVisibility(View.INVISIBLE);
				mEdtTextSearch.getText().clear();
				mBtnClearText.setVisibility(View.GONE);
			}
		});
		
		mBtnSearchInternet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final String keyword = mEdtTextSearch.getText().toString();
				if(keyword.trim().length()==0) {
					GeneralUtils.showToastMessage(getActivity(), getActivity().getString(R.string.mess_no_search_info));
				} else {

					final View childView = LayoutInflater.from(getActivity()).inflate(R.layout.custom_dialog_simple_layout, null, false);
					TextView textView = (TextView)childView.findViewById(R.id.tv_mess_content);
					textView.setText(getActivity().getString(R.string.mess_online_search_confirm)
							+ " '" + keyword + "' ?");
					final CustomAlertDialog alert = new CustomAlertDialog(getActivity(), childView, true);
					alert.setTitle(getActivity().getString(R.string.title_online_search_confirm));
					alert.setButton(getActivity().getString(R.string.button_text_ok), new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							doInternetSearch(keyword);
							alert.hideDialog();
						}
					});
					alert.setButton2(getActivity().getString(R.string.button_text_cancel), new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							alert.hideDialog();
						}
					});
					alert.showDialog();
				
				}
			}
		});

		return view;
	}
	
	private void doInternetSearch(String keyword) {
		
		String query = null;
		try {
			query = URLEncoder.encode(keyword, "utf-8");
		} catch (UnsupportedEncodingException e) {
			query = keyword;
			e.printStackTrace();
		}
		String url = "http://www.google.com/search?q=" + query;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(url));
		startActivity(intent);
	}

//	private String[] getAllSongTitle() {
//		lstTitle = new ArrayList<String>();
//		lstTitle = insSongHelper.getAllTitle();
//		String[] result = new String[lstTitle.size()];
//		for (int i = 0; i < lstTitle.size(); i++) {
//			result[i] = lstTitle.get(i);
//		}
//		return result;
//	}

	private String[] getAllSongTitleOnFilter(String filter) {
		lstTitle = new ArrayList<String>();
		lstTitle = insSongHelper.getAllTitleOnFilter(filter);
		String[] result = new String[lstTitle.size()];
		for (int i = 0; i < lstTitle.size(); i++) {
			result[i] = lstTitle.get(i);
		}
		return result;
	}

//	private String[] getAllSongNumber() {
//		lstNumber = new ArrayList<String>();
//		lstNumber = insSongHelper.getAllNumber();
//		String[] result = new String[lstNumber.size()];
//		for (int i = 0; i < lstNumber.size(); i++) {
//			result[i] = lstNumber.get(i);
//		}
//		return result;
//	}

	private String[] getAllSongNumberOnFilter(String filter) {
		lstNumber = new ArrayList<String>();
		lstNumber = insSongHelper.getAllNumberOnFilter(filter);
		String[] result = new String[lstNumber.size()];
		for (int i = 0; i < lstNumber.size(); i++) {
			result[i] = lstNumber.get(i);
		}
		return result;
	}

}
